﻿// 21. Write a program that reads two numbers N and K and generates all the combinations of K distinct elements from the set [1..N]. 
// Example:
//	N = 5, K = 2 -> {1, 2}, {1, 3}, {1, 4}, {1, 5}, {2, 3}, {2, 4}, {2, 5}, {3, 4}, {3, 5}, {4, 5}

using System;

class CombinationsOfKDistinctElements
{
    static void Main()
    {

        // get number N
        Console.Write("Enter number N : ");
        string numberNStr = Console.ReadLine();
        int numberN;
        while (!int.TryParse(numberNStr, out numberN))
        {
            Console.Write("Invalid input. Enter N again : ");
            numberNStr = Console.ReadLine();
        }

        // get number K
        Console.Write("Enter number K : ");
        string numberKStr = Console.ReadLine();
        int numberK;
        while (!int.TryParse(numberKStr, out numberK))
        {
            Console.Write("Invalid input. Enter K again : ");
            numberKStr = Console.ReadLine();
        }

        if (numberN < 0 || numberK < 0)
        {
            Console.WriteLine("Invalid input. The numbers should be positive. Start the program again.");
        }
        else
        {
            int[] array = new int[numberK];
            Variations(array, 0, numberN, 1); //Call Method
        }
    }

    static void Variations(int[] array, int index, int n, int k)
    {
        if (index == array.Length) //Bottom
        {
            for (int i = 0; i < array.Length; i++)
            {
                Console.Write(array[i] + " ");
            }
            Console.WriteLine();
        }
        else
        {
            for (int j = k; j <= n; j++) //Call Recursion
            {
                array[index] = j;
                Variations(array, index + 1, n, j + 1);
            }
        }
    }
}